package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckitemDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: bowen
 * @Date: 2019/6/22 15:50
 * @Description:
 */
@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemDao checkitemDao;

    /**
     *  @描述：新增检查项
     * @param checkItem
     */
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);
    }

    /**
     *  @描述：分页查询
     */
    public PageResult findPage(Integer pageSize, Integer currentPage, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page=checkitemDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     *  @描述：删除
     */
    public void delete(Integer id) {
        //判断检查项是否被引用
       Long count= checkitemDao.findCountByCheckitemId(id);
       if(count>0){
           //如果被引用，报错
           throw new RuntimeException(MessageConstant.CHECKITEM_IS_CALLED);
       }
        //没有被引用，删除
        checkitemDao.delete(id);
    }

    /**
     *  @描述：根据id查询检查项
     */
    public CheckItem findById(Integer id) {
        return checkitemDao.findById(id);
    }

    /**
     *  @描述：编辑检查项
     */
    public void edit(CheckItem checkItem) {
        checkitemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }
}
